export function baseRequest<T>(url: string, options?: RequestInit): Promise<T> {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open(options.method, url, true);
        xhr.send(options.body || null);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    resolve(JSON.parse(xhr.responseText))
                } else {
                    reject(xhr.responseText)
                }
            }
        };
    })

}

export function isPromise(val: any): boolean {
    return val && typeof val.then === "function";
}

export function isString(val: any): boolean {
    return typeof val === "string";
}

export function isNumber(val: any): boolean {
    return typeof val === "number";
}

export function isBoolean(val: any): boolean {
    return typeof val === "boolean";
}

export function isFunction(val: any): boolean {
    return typeof val === "function";
}
/**
 *  首字母大写
 * @param str    
 * @return 
 */
export function capitalizeFirstLetter<T extends string | null | undefined>(str: T): T {
    if (!str) return str;
    return str.charAt(0).toUpperCase() + str.slice(1) as T;
}

/**
 *  获取字符串中的数字
 * @param string 
 * @return number
 */
export function getNumberFromString(str: string | number): number {
    if (typeof str === 'number') return str;
    return Number(str.replace(/[^0-9]/ig, ""));
}
